package com.bw.cement.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.base.constants.UserConst
import com.bw.cement.adapter.CementAdapter
import com.bw.cement.databinding.FragmentCementBinding
import com.bw.cement.viewmodel.CementViewModel
import com.bw.common.entity.CustomEvent
import com.bw.common.view.BaseMvvmFragment
import com.tencent.mmkv.MMKV
import org.greenrobot.eventbus.EventBus


@Route(path = RoutePath.PATH_CEMENT_FRAGMENT)
class CementFragment : BaseMvvmFragment<FragmentCementBinding,CementViewModel>() {

    private var uid:Int = 0
    private lateinit var adapter: CementAdapter

    override fun initView() {
        adapter = CementAdapter()
        binding.rvCement.adapter = adapter
        binding.rvCement.layoutManager = LinearLayoutManager(context)

    }

    override fun initData() {
        vm.cementSuccess.observe(this){

            adapter.data.clear()
            adapter.data.addAll(it)
            adapter.notifyDataSetChanged()
            EventBus.getDefault().post(CustomEvent("MainActivity",it.size,3))

        }
    }

    override fun onResume() {
        super.onResume()
        if(adapter.data.isEmpty()){
            uid = MMKV.defaultMMKV().decodeInt(UserConst.USER_ID)
            vm.getCementsByUid(uid)
        }
    }

}